package com.echat.serviceapigateway.security.controller;

import com.echat.serviceapigateway.security.common.HttpResult;
import com.echat.serviceapigateway.security.model.authentication.Token;
import com.echat.serviceapigateway.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chenmohan on 2018/08/30
 *
 * @description token认证相关的Controller
 */
@RestController
@RequestMapping("/auth")
public class AuthenticateController {

    @Autowired
    TokenService tokenService;

    /**
     * 使用accessKey和accessSecret获取accessToken
     * @param accessKey
     * @param accessSecret
     * @return
     */
    @RequestMapping("/accquire")
    public HttpResult<Token> accquireToken(String accessKey, String accessSecret) {
        Token accessToken = tokenService.accquireAccessToken(accessKey, accessSecret);
        return HttpResult.token(accessToken);
    }


    //@RequestMapping("/refresh") 暂时不使用refreshToken相关功能
    public HttpResult<Token> refreshToken(String accessKey, String refreshToken) {
        Token accessToken = tokenService.refreshAccessToken(accessKey, refreshToken);
        return HttpResult.success(accessToken);
    }

    /**
     * 查询accessToken的有效时间
     * @return  【直接返回Long对象】
     */
    @RequestMapping("/ttl")
    public Long getTTL(String accessToken){
        return tokenService.getTTL(accessToken);
    }

}
